package com.kanche.bigbaby.collection.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

import com.kanche.bigbaby.R;

//import com.kkche.merchant.R;

/**
 * Created by zhangsl on 15/1/14.
 */
public class EvaluateView extends FrameLayout implements View.OnClickListener {
    private TextView module_title, module_content;
    private ImageButton[] checkArrays = new ImageButton[3];
    private int[] checkID = {R.id.evaluatestar_1, R.id.evaluatestar_2, R.id.evaluatestar_3};
    private OnStarChangListener onStarChangListener;
    private String[] stringValue;
    private int starnumOld;
    private int starnum;

    public EvaluateView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.evaluate, this);
        stringValue = getResources().getStringArray(R.array.star_value);
        initView();
    }


    private void initView() {
        module_title = (TextView) findViewById(R.id.module_title);
        module_content = (TextView) findViewById(R.id.module_content);
        for (int i = 0; i < checkArrays.length; i++) {
            checkArrays[i] = (ImageButton) findViewById(checkID[i]);
            checkArrays[i].setTag(i);
            checkArrays[i].setOnClickListener(this);
        }

    }

    public void setTitle(String title) {
        module_title.setText(title);
    }

    @Override
    public void onClick(View v) {
        int tag = (Integer) v.getTag();
        starnumOld = starnum;
        if (tag + 1 < starnum) {
            for (int i = tag + 1; i < starnum; i++) {
                checkArrays[i].setImageResource(R.drawable.star_normal);
            }
            starnum = tag + 1;
        } else if (tag + 1 == starnum) {
            checkArrays[tag].setImageResource(R.drawable.star_normal);
            starnum = tag;
        } else if (tag + 1 > starnum) {
            for (int i = starnum; i <= tag; i++) {
                checkArrays[i].setImageResource(R.drawable.star_pressed);
            }
            starnum = tag + 1;
        }
        setStarnum(starnum);
        if (null != onStarChangListener) {
            onStarChangListener.changStra(this, starnum);
        }
    }


    public void setStarnum(int starnum) {
        this.starnum = starnum;
        for (int i = 0; i < 3; i++) {
            if (i < starnum) {
                checkArrays[i].setImageResource(R.drawable.star_pressed);
            } else {
                checkArrays[i].setImageResource(R.drawable.star_normal);
            }
        }
        module_content.setText(stringValue[starnum]);
        if (starnum > 0) {
            module_title.setTextColor(Color.parseColor("#00bc56"));
        } else {
            module_title.setTextColor(Color.parseColor("#525459"));
        }
    }

    public void setOldStarnum() {
        setStarnum(starnumOld);
        starnum = starnumOld;
    }

    public void setOnStarChangListener(OnStarChangListener onStarChangListener) {
        this.onStarChangListener = onStarChangListener;
    }

    public interface OnStarChangListener {
        void changStra(View view, int starnum);
    }
}
